package com.bvbot.core.extractor;

import static com.bvbot.core.Logger.log;

import java.math.BigDecimal;
import java.util.Date;

import com.bvbot.core.bot.Prices;
import com.bvbot.core.session.BvSession;
import com.bvbot.core.session.BvSessionException;
import com.bvbot.core.session.MarketResponse;

public class BvExtractor implements PriceExtractor {

	private static final String GALMARLEY_GOLD_PRICE_URL = "http://www.galmarley.com/prices/CHART_LINE/AUX/USD/5/Update";

	private static final String GALMARLEY_SILVER_PRICE_URL = "http://www.galmarley.com/prices/CHART_LINE/AGX/USD/5/Update";

	public BvExtractor() {
	}

	private Float getWorldValue(String galmarleyHtml) {
		Float result = null;
		if (galmarleyHtml != null) {
			int idxPriceStartTag = galmarleyHtml.indexOf("<price>");
			int idxPriceEndTag = galmarleyHtml.indexOf("</price>");
			if ((idxPriceEndTag >= 0) && (idxPriceStartTag >= 0)) {
				String priceStringNotTrimmed = galmarleyHtml.substring(
						idxPriceStartTag + 7, idxPriceEndTag);
				String trimmedPrice = priceStringNotTrimmed.trim();
				try {
					result = Float.parseFloat(trimmedPrice);
				} catch (NumberFormatException e) {
					log("Impossible to extract price from string "
							+ trimmedPrice);
				}
			}
		} else {
			log("Impossible to extract current galmarley price");
		}
		return result;
	}

	@Override
	public Prices extract() {
		// result
		Prices result = new Prices();
		try {
			UrlTool ex = new UrlTool();
			// gold world value
			String galmarleyHtml = ex.getHtml(GALMARLEY_GOLD_PRICE_URL);
			float worldGoldValue = getWorldValue(galmarleyHtml);
			result.setGoldWorldValue(worldGoldValue);
			// silver world value
			galmarleyHtml = ex.getHtml(GALMARLEY_SILVER_PRICE_URL);
			float worldSilverValue = getWorldValue(galmarleyHtml);
			result.setSilverWorldValue(worldSilverValue);
			// bv markets
			BvSession session = new BvSession();
			// gold
			MarketResponse mr = session.getGoldMarket();
			float bvGoldBuyValue = toFloat(mr.computeLowestSellPrice());
			float bvGoldSellValue = toFloat(mr.computeHighestBuyPrice());
			result.setBvGoldBuyValue(bvGoldBuyValue);
			result.setBvGoldSellValue(bvGoldSellValue);
			// silver
			mr = session.getSilverMarket();
			float bvSilverBuyValue = toFloat(mr.computeLowestSellPrice());
			float bvSilverSellValue = toFloat(mr.computeHighestBuyPrice());
			result.setBvSilverBuyValue(bvSilverBuyValue);
			result.setBvSilverSellValue(bvSilverSellValue);
			result.setPricesDate(new Date());
		} catch (BvSessionException e) {
			log("Exception : " + e.getMessage(), e);
		}
		return result;
	}

	private Float toFloat(BigDecimal price) {
		Float result = null;
		if (price != null) {
			result = price.floatValue();
		}
		return result;
	}

	public static void main(String[] args) {
		BvExtractor e = new BvExtractor();
		Prices prices = e.extract();
		if (prices != null) {
			log("Current prices are " + prices);
		} else {
			log("Impossible to extract current bv prices.");
		}
	}

}
